package by.dkusch.aircompany.webapp.page.staff.panel;

import javax.inject.Inject;
import javax.persistence.metamodel.SingularAttribute;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.OrderByBorder;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.SortOrder;
import org.apache.wicket.extensions.markup.html.repeater.data.table.filter.FilterForm;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.EnumLabel;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.model.PropertyModel;

import by.dkusch.aircompany.model.filter.FilterPerson;
import by.dkusch.aircompany.model.tables.Person;
import by.dkusch.aircompany.model.tables.Person_;
import by.dkusch.aircompany.model.tables.enums.Gender;
import by.dkusch.aircompany.model.tables.enums.PersonPosition;
import by.dkusch.aircompany.services.PersonService;
import by.dkusch.aircompany.webapp.page.base.BaseLayout;
import by.dkusch.aircompany.webapp.page.base.common.AddNewItemPanel;
import by.dkusch.aircompany.webapp.page.base.common.CommonActionPanel;
import by.dkusch.aircompany.webapp.page.base.common.EntityDataProviderFilter;
import by.dkusch.aircompany.webapp.page.base.panels.MenuPanel;
import by.dkusch.aircompany.webapp.page.staff.PersonPage;

public class PersonListPanel extends Panel {

	private static final long serialVersionUID = 1L;
	@Inject
	private PersonService personService;
	private EntityDataProviderFilter<Long, Person, FilterPerson> personDataProvider;

	public PersonListPanel(String id, int itemsPerPage) {
		super(id);

		personDataProvider = new EntityDataProviderFilter<Long, Person, FilterPerson>(personService);
		personDataProvider.setSort(Person_.tabCode, SortOrder.ASCENDING);
		personDataProvider.setFilterState(new FilterPerson());

		setOutputMarkupId(true);

		DataView<Person> dataView = new DataView<Person>("personList", personDataProvider, itemsPerPage) {
			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(Item<Person> item) {
				final Person person = item.getModelObject();
				item.add(new CheckBox("arcFl").setEnabled(false));
				item.add(new Label("tabCode"));
				item.add(new Label("uFIO"));
				item.add(new EnumLabel<PersonPosition>("posId"));
				item.add(new EnumLabel<Gender>("gender"));
				item.add(new CommonActionPanel<Long, Person, PersonPage>("action-panel", personService, person, PersonPage.class, PersonListPanel.this));

			}
		};


		FilterForm<FilterPerson> filterForm = new FilterForm<FilterPerson>("filterForm", personDataProvider);
		filterForm.add(new TextField<String>("filter-code", PropertyModel.<String> of(personDataProvider, "filterState.tabCode")));
		filterForm.add(new TextField<String>("filter-FIO", PropertyModel.<String> of(personDataProvider, "filterState.FIO")));
		filterForm.add(new TextField<String>("filter-firstName", PropertyModel.<String> of(personDataProvider, "filterState.firstName")));
		filterForm.add(new TextField<String>("filter-secondName", PropertyModel.<String> of(personDataProvider, "filterState.secondName")));
		filterForm.add(new TextField<String>("filter-lastName", PropertyModel.<String> of(personDataProvider, "filterState.lastName")));

		final WebMarkupContainer divTable = new WebMarkupContainer("div-table");
		final PagingNavigator pagingNavigator = new PagingNavigator("paging", dataView);
		divTable.setOutputMarkupId(true);
		pagingNavigator.setOutputMarkupId(true);

		filterForm.add(new AjaxButton("personal-search", filterForm) {
			/**
				 * 
				 */
			private static final long serialVersionUID = 1L;

			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				// repaint the feedback panel so that it is hidden
				target.add(((MenuPanel) ((BaseLayout) getPage()).getMenuPanel()).getFeedback());
				target.add(divTable);
				target.add(pagingNavigator);
			}

			@Override
			protected void onError(AjaxRequestTarget target, Form<?> form) {
				// repaint the feedback panel so errors are shown
				target.add(((MenuPanel) ((BaseLayout) getPage()).getMenuPanel()).getFeedback());
				target.add(divTable);
			}
		});

		divTable.add(dataView);
		divTable.add(new AddNewItemPanel<Long, Person, PersonPage>("new-panel", new Person(), PersonPage.class));

		divTable.add(new OrderByBorder<SingularAttribute<Person, ?>>("sortByFIO", Person_.firstName, personDataProvider));
		divTable.add(new OrderByBorder<SingularAttribute<Person, ?>>("sortByCode", Person_.tabCode, personDataProvider));
		divTable.add(new OrderByBorder<SingularAttribute<Person, ?>>("sortByPosition", Person_.posId, personDataProvider));
		divTable.add(new OrderByBorder<SingularAttribute<Person, ?>>("sortByGender", Person_.gender, personDataProvider));

		filterForm.add(divTable);
		add(pagingNavigator);
		add(filterForm);
	}

}
